home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
print
/
p1123mz4.zip
/
HEXMAZE.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-14
|
49KB
|
1,236 lines
#include <stdlib.h>
#include <iostream.h>
#include "oracle.h"
#include "cell.h"
#include "titillat.h"
#include "hexmaze.h"
#define TRUE -1
#define FALSE 0
typedef cell *cell_ptr;
typedef char *char_ptr;
maze::maze(int row_count,int column_count,int thickness_of_wall,char *seed)
// Contruct a maze having "row_count" rows and "column_count" columns of
// rooms. The walls should be "thickness_of_wall" (bricks) thick. A different
// (8 character of less) "seed" generally yields a different maze.
{
struct
{
int row_num;
int column_num;
} delta [2] [6];
int mud_filled_room_found;
struct
{
int row_num;
int column_num;
} next;
char wall [720] [6];
char wall_to_check;
char wall_num;
char way_out;
int x1;
int x2;
int y1;
int y2;
wall_thickness=thickness_of_wall;
num_rows=row_count;
num_y_dots=wall_thickness*(4*num_rows+1);
y_dot_max=num_y_dots-1;
num_columns=column_count;
max_x=8*(num_columns/2)+6;
num_x_dots=wall_thickness*(max_x+1);
x_dot_max=num_x_dots-1;
// Allocate a two dimensional array of rooms.
if (memory_allocated=((room=new cell_ptr[num_rows]) != NULL))
{
int row_num=0;
while (memory_allocated && (row_num < num_rows))
if (memory_allocated=((room[row_num]=new cell [num_columns]) != NULL))
row_num++;
if (! memory_allocated)
{
while (row_num)
delete [] room[--row_num];
delete [] room;
cerr << "Fatal error: cannot allocate maze.\n";
}
}
if (memory_allocated)
{
// Allocate a two dimensional array for plotting the maze in
// terms of "bricks" where a wall is "wall_thickness" bricks
// thick.
if (memory_allocated=((page=new char_ptr [num_y_dots]) != NULL))
{
int y_dot_num=0;
while (memory_allocated && (y_dot_num < num_y_dots))
if (memory_allocated=((page[y_dot_num]=new char [num_x_dots])
!= NULL))
y_dot_num++;
if (! memory_allocated)
{
while (y_dot_num)
delete [] page[--y_dot_num];
delete [] page;
for (int row_num=num_rows-1; row_num >= 0; row_num--)
delete [] room[row_num];
delete [] room;
cerr << "Fatal error: cannot allocate maze.\n";
}
}
else
{
for (int row_num=num_rows-1; row_num >= 0; row_num--)
delete [] room[row_num];
delete [] room;
cerr << "Fatal error: cannot allocate maze.\n";
}
}
if (memory_allocated)
{
// Set up the directions by which a room can be exited.
// Directions for even columns.
delta[0][0].row_num=-1; // north
delta[0][0].column_num=0;
delta[0][1].row_num=-1; // northwest
delta[0][1].column_num=-1;
delta[0][2].row_num=0; // southwest
delta[0][2].column_num=-1;
delta[0][3].row_num=1; // south
delta[0][3].column_num=0;
delta[0][4].row_num=0; // southeast
delta[0][4].column_num=1;
delta[0][5].row_num=-1; // northeast
delta[0][5].column_num=1;
// Directions for odd columns.
delta[1][0].row_num=-1; // north
delta[1][0].column_num=0;
delta[1][1].row_num=0; // northwest
delta[1][1].column_num=-1;
delta[1][2].row_num=1; // southwest
delta[1][2].column_num=-1;
delta[1][3].row_num=1; // south
delta[1][3].column_num=0;
delta[1][4].row_num=1; // southeast
delta[1][4].column_num=1;
delta[1][5].row_num=0; // northeast
delta[1][5].column_num=1;
// Set up the 6! orders in which the wall of a room can be accessed.
int order_num=0;
for (int wall_num_1=0; wall_num_1 < 6; wall_num_1++)
for (int wall_num_2=0; wall_num_2 < 6; wall_num_2++)
if (wall_num_2 != wall_num_1)
for (int wall_num_3=0; wall_num_3 < 6; wall_num_3++)
if ((wall_num_3 != wall_num_2)
&& (wall_num_3 != wall_num_1))
for (int wall_num_4=0; wall_num_4 < 6; wall_num_4++)
if ((wall_num_4 != wall_num_3)
&& (wall_num_4 != wall_num_2)
&& (wall_num_4 != wall_num_1))
for (int wall_num_5=0; wall_num_5 < 6; wall_num_5++)
if ((wall_num_5 != wall_num_4)
&& (wall_num_5 != wall_num_3)
&& (wall_num_5 != wall_num_2)
&& (wall_num_5 != wall_num_1))
for (int wall_num_6=0; wall_num_6 < 6; wall_num_6++)
if ((wall_num_6 != wall_num_5)
&& (wall_num_6 != wall_num_4)
&& (wall_num_6 != wall_num_3)
&& (wall_num_6 != wall_num_2)
&& (wall_num_6 != wall_num_1))
{
wall[order_num][wall_num_6]='\0';
wall[order_num][wall_num_5]='\1';
wall[order_num][wall_num_4]='\2';
wall[order_num][wall_num_3]='\3';
wall[order_num][wall_num_2]='\4';
wall[order_num][wall_num_1]='\5';
order_num++;
}
titillator_ptr=new titillator;
order_selector=new oracle(&seed[0],order_num);
row_selector=new oracle(&seed[0],num_rows);
column_selector=new oracle(&seed[0],num_columns);
int finished=FALSE;
// Generate mazes until you have one that is hard to solve.
do
{
// Pick a starting room.
first.column_num=column_selector->random_number();
if ((first.column_num)%2)
do
{
first.row_num=row_selector->random_number();
}
while (first.row_num == (num_rows-1));
else
first.row_num=row_selector->random_number();
current.row_num=first.row_num;
current.column_num=first.column_num;
// Excavate the starting room.
room[current.row_num][current.column_num].mark_floor(' ');
// Pick the order in which the walls of the starting room will be
// considered.
room[current.row_num][current.column_num].set_order(
order_selector->random_number());
titillator_ptr->titillate();
// Excavate rooms until there are no more to excavate.
do
{
mud_filled_room_found=FALSE;
// Find an adjacent room (not yet considered) that needs
// excavating.
do
{
wall_num=room[current.row_num][
current.column_num].next_wall_num();
if (wall_num < '\6')
{
wall_to_check=wall[room[current.row_num][
current.column_num].order_to_check()][wall_num];
if (room[current.row_num][
current.column_num].wall_up(wall_to_check))
{
next.column_num=current.column_num